
cap cd "/Users/kevin/Dropbox/TZ ITNs"
set more off
set varabbrev off

run "district malaria data prep.do"

run "create 2008 data for merge.do"

******
*NPS SECTION A: Interview details
*rename basic interview time and location variables
*******

use "2010-11 data/TZNPS2HH1DTA/HH_SEC_A.dta", clear

rename y2_rural rural
gen urban=1-rural
rename hh_a18_month ivu_month
rename hh_a18_year ivu_year
sort y2_hhid

******
*SECTION B: Household Roster
*******

merge 1:m y2_hhid using "2010-11 data/TZNPS2HH1DTA/HH_SEC_B.dta", nogen
gen hhid=hhid_2008
destring hhid, replace

egen personid_2010=concat(y2_hhid indidy2)
sort personid_2010

rename (hh_b02 hh_b04) (sex age)
gen under5= (age<5)
gen female= (sex==2)

save "constructed data/2010/SEC_B.dta", replace

******
**NPS Section D: Health module
*create variables for bed net ownership and source of bed net
******

use "2010-11 data/TZNPS2HH1DTA/HH_SEC_D.dta", clear 
rename hh_* *
egen personid_2010=concat(y2_hhid indidy2)
sort personid_2010

rename (d01 d31) (respondent_himself net)
gen free_net= (d32==1) if !missing(d32)
gen free_net_all= (d32==1)
gen voucher_net= (d32==3) if !missing(d32)
gen free_voucher= (free_net==1 | voucher_net==1) if !missing(d32)
gen free_itn= (free_net==1) & (net==2 | net==3) if !missing(free_net) & !missing(net)
gen free_itn_all= (free_net==1) & (net==2 | net==3)

bys y2_hhid: egen free_hh= max(free_net) if !missing(free_net)
bys y2_hhid: egen free_hh_all= max(free_net_all)
bys y2_hhid: egen freevoucher_hh= max(free_voucher) if !missing(free_voucher) 
bys y2_hhid: egen voucher_hh= max(voucher_net) if !missing(voucher_net)

gen slept_net= (net<=3) if !missing(net)
gen slept_itn= (net==2 | net==3) if !missing(slept_net)
bys y2_hhid: egen anynet=max(slept_net)
bys y2_hhid: egen free_itnhh=max(free_itn) if !missing(free_itn)
bys y2_hhid: egen anyslept_itn=max(slept_itn)
bys y2_hhid: egen itn_pct=mean(slept_itn)

sort personid_2010

save "constructed data/2010/sectionD.dta", replace

******
*Section C: Education module
*****

use "2010-11 data/TZNPS2HH1DTA/HH_SEC_C.dta", clear 
egen personid_2010=concat(y2_hhid indidy2)
sort personid_2010
rename hh_* *

merge 1:1 personid_2010 using "constructed data/2010/SEC_B.dta", nogen

merge 1:1 personid_2010 using "constructed data/2010/sectionD.dta", nogen

***keep relevant variables****
keep y2_hhid indidy2 personid_2010 rural urban sex female respondent_himself ivu_month ///
ivu_year region district ward ea ///
age hhid hhid_2008 under5 free_net-itn_pct

save "constructed data/2010/SEC_BCD.dta", replace

******
/*SECTION H: Governance module
Reshape for merging, and for approval outcome variables,
replace  "don't know" (=5) as missing
and create binary version of the variable for approve or strongly approved (=1 or =2)
*/
*******

use "2010-11 data/TZNPS2HH1DTA/HH_SEC_H1.dta", clear
drop hh_h04_2 // need to drop this for reshape to work

drop if hhid=="0703018007004201" //*many duplidates which is preventing reshape

gen itemcode1=itemcode
replace itemcode1="1" if itemcode=="A"
replace itemcode1="2" if itemcode=="B"
replace itemcode1="3" if itemcode=="C"
replace itemcode1="4" if itemcode=="D"
replace itemcode1="5" if itemcode=="E"
replace itemcode1="6" if itemcode=="F"
replace itemcode1="7" if itemcode=="G"
replace itemcode1="8" if itemcode=="H"

destring itemcode1, replace
drop itemcode

reshape wide hh_h05 hh_h06, i(hhid) j(itemcode1)
destring hhid, replace
rename hh_* *

rename (h061 h062 h063 h064 h068 ) (approve_vc approve_veo approve_weo approve_councillor approve_mp)
rename (h065 h066 h067) (approve_headmaster approve_extofficer approve_police) 

foreach var in approve_mp approve_police approve_extofficer approve_headmaster ///
approve_veo approve_vc approve_councillor approve_weo {
replace `var'=. if (`var'==5 | `var'==9)
gen `var'1= (`var'==1 | `var'==2) if !missing(`var')
}

sort y2_hhid

save "constructed data/2010/H1.dta", replace

******
*SECTION H: Governance module part 2: reshape for merging
*******

use "2010-11 data/TZNPS2HH1DTA/HH_SEC_H2.dta", clear

gen itemcode2=itemcode
replace itemcode2="1" if itemcode=="A"
replace itemcode2="2" if itemcode=="B"
replace itemcode2="3" if itemcode=="C"
replace itemcode2="4" if itemcode=="D"
replace itemcode2="5" if itemcode=="E"

rename hh_* *

drop h07_2

destring itemcode2, replace
drop itemcode

reshape wide h07_3 h08, i(y2_hhid) j(itemcode2)

rename (h07_31 h081 h07_32 h082 h07_33 h083 h07_34 h084 h07_35 h085) ///
(kitongoji kitongoji_reason village village_reason farmer_coop coop_reason sacco sacco_reason smc smc_reason) 

gen hhid=y2_hhid
destring hhid, replace

save "constructed data/2010/H2.dta", replace

use "2010-11 data/TZNPS2HH1DTA/HH_SEC_H3.dta", clear

gen itemcode3=itemcode
replace itemcode3="1" if itemcode=="A"
replace itemcode3="2" if itemcode=="B"
replace itemcode3="3" if itemcode=="C"
replace itemcode3="4" if itemcode=="D"
replace itemcode3="5" if itemcode=="E"

destring itemcode3, replace
drop itemcode hh_h09_2

reshape wide hh_h09_3, i(y2_hhid) j(itemcode3)

rename hh_* *
gen hhid=y2_hhid
destring hhid, replace

merge 1:1 hhid using "constructed data/2010/H2.dta", nogen

merge 1:1 y2_hhid using "constructed data/2010/H1.dta", nogen

******
*consumption aggregates: provided by NPS team in public data release
*generate logged per capita consumption variable
*******

merge 1:1 y2_hhid using "2010-11 data/TZNPS2HH2DTA/TZY2.HH.Consumption.dta", nogen

rename expmR consumption
gen consumption_pc=consumption/adulteq
gen lconsumption=ln(consumption_pc)
lab var lconsumption "consumption per capita (log), 2010"

*********
*merge in filter variables so we know who answered the political questions
*********

merge 1:1 y2_hhid using "2010-11 data/TZNPS2HH1DTA/HH_SEC_FILTERS.dta", nogen

rename (hh_h01 hh_h03) (gov_respondent_selected gov_respondent_replace)
gen gov_respondent_2010=gov_respondent_selected if hh_h02==1
replace gov_respondent_2010=gov_respondent_replace if hh_h02==2
lab var gov_respondent_2010 "roster no of respondent selected for governance module"

keep y2_hhid hhid hhid_2008 approve* consumption lconsumption consumption_pc mainland gov_respondent* intmonth  ///
intyear urban

save "constructed data/2010/H123_consumption.dta", replace

**********
*Link variable (links community questionnaire to household questionnaire
**********

use "2010-11 data/TZNPS2COMDTA/y2commlink.dta", clear 
egen temp=concat( id_01 id_02 id_03 id_04)

save "constructed data/2010/commlink.dta", replace

*******
/* Merge community files
Create and label variables which show which party the village chair is a member of.
Exclude cases where the interviewed leader is a non-village level leader (WEO, councillor, MP)
Calculate the percentages of CCM, CUF, and CHADEMA leaders where there are multiple leaders interviewed.
*/
*******
 
use "2010-11 data/TZNPS2COMDTA/COMSEC_CI.dta", clear
 
sort id_01 id_02 id_03 id_04
drop temp 
rename (cm_i01 cm_i06) (leader_type leader_party)
replace leader_party=. if leader_party==0

gen ccm= (leader_party==1) if !missing(leader_party)
label var ccm "village official party is CCM"
gen chadema= (leader_party==2) if !missing(leader_party)
label var chadema "village official party is Chadema"
gen cuf= (leader_party==3) if !missing(leader_party)
label var cuf "village official party is CUF"
gen party_other= (leader_party==4) if !missing(leader_party)
gen village_level= (leader_type<=13) if !missing(leader_type)

egen temp=concat( id_01 id_02 id_03 id_04)
bys temp: gen tempindex=_n

reshape wide leader_type-village_level, i(temp) j(tempindex)

order leader_party*, first
egen number_leaders=rownonmiss(leader_party*)

egen vb_chair_listed=anycount(leader_type*), v(13)
egen n_high_off_listed=anycount(leader_type*), v(15/17)
egen vill_level_listed=anycount(leader_type*), v(1/13)

forvalues i=1/37 {
gen ccm_vill_lead`i'= (leader_party`i'==1 & leader_type`i'<15) if !missing(leader_party`i') & !missing(leader_type`i')
gen chadema_vill_lead`i'= (leader_party`i'==2 & leader_type`i'<15) if !missing(leader_party`i') & !missing(leader_type`i')
gen cuf_vill_lead`i'= (leader_party`i'==3 & leader_type`i'<15) if !missing(leader_party`i') & !missing(leader_type`i')
gen other_vill_lead`i'= (leader_party`i'==4 & leader_type`i'<15) if !missing(leader_party`i') & !missing(leader_type`i')
}

egen ccm_pct=rowmean(ccm_vill_lead*) 
egen chadema_pct=rowmean(chadema_vill_lead*)
egen cuf_pct=rowmean(cuf_vill_lead*)
egen other_pct=rowmean(other_vill_lead*)
egen total_opp_pct=rowtotal(chadema_pct cuf_pct other_pct), m

foreach v in ccm_pct chadema_pct cuf_pct other_pct {
gen `v'_0= (`v'>0 & `v'<=0.25) if !missing(`v')
gen `v'_25= (`v'>0.25 & `v'<=0.50) if !missing(`v')
gen `v'_50= (`v'>0.5 & `v'<=0.75) if !missing(`v')
gen `v'_75=(`v'>0.75 & `v'<1) if !missing(`v')
gen `v'_100=( `v'==1) if !missing(`v')
}

gen ccm_majority_2010=ccm_pct>.5 if !missing(ccm_pct)
lab var ccm_majority_2010 "more than 50% village officials listed in 2010 were CCM" 
gen ccm_50_2010=ccm_pct>.5 if !missing(ccm_pct)
lab var ccm_50_2010 "more than 50% village officials listed in 2010 were CCM" 
gen ccm_100_2010=ccm_pct==1 if !missing(ccm_pct)
lab var ccm_100_2010 "100% village officials listed in 2010 were CCM"
gen opposition_50_2010=total_opp_pct>.5 if !missing(total_opp_pct)
lab var opposition_50_2010 ">50% village officials listed in 2010 were opposition"
 
save "constructed data/2010/CI.dta", replace

merge 1:m temp using "constructed data/2010/commlink.dta"

/*
84% good merges, _merge=3= 3,276. Why? There were 3,265 respondents in 2008. 
These missing are likely people who moved to non-survey areas between survey rounds,
and they were tracked but so their community was not surveyed in the follow up round. We drop these.
*/

drop if _merge~=3
drop _merge
sort y2_hhid
gen ea_id08=temp
bys ea_id08: gen nvals= _n

save "constructed data/2010/CI_link.dta", replace

use "constructed data/2010/H123_consumption.dta", replace

merge m:1 y2_hhid using "constructed data/2010/CI_link.dta", nogen

keep ccm* cuf* chadema* opposition* party* other* total* ea* temp y2_hhid hhid_2008 mainland id* gov_* ///
approve*  consumption* lconsumption hhid urban int*

merge 1:1 y2_hhid using "2010-11 data/TZNPS2GEODTA/HH.Geovariables_Y2.dta", nogen

save "constructed data/2010/H123_consumption_comm_geo.dta", replace

use "constructed data/2010/SEC_BCD.dta", clear

merge m:1 y2_hhid using "constructed data/2010/H123_consumption_comm_geo.dta", nogen

gen ivu_day=15
egen monthyear=concat(ivu_month ivu_day ivu_year), punct(/)
gen ivudate=date(monthyear, "MDY")
format ivudate %d
gen cycle= ((ivudate<=td(15,04,2011)))

destring temp, replace 

drop soil* crops* clim*

save "constructed data/2010/H123_consumption_comm_geo_clean.dta", replace

****
*merge with 2008 data, with district names from ArcGIS, and with district-level malaria data
****

merge m:1 hhid_2008 using "constructed data/2008/vill_pol_for_2010_merge_prep.dta", nogen

merge m:1 ea_id using "constructed data/ea_from_dbase.dta", nogen

merge m:1 DISTNAME using "constructed data/districts_malaria", nogen

*drop observations from Zanzibar, since the ITN campaign was not conducted there
drop if mainland==2

*********************
**This is the final dataset on which analysis can be run
*now main analysis variables can be created
*********************

*generate a unique district identifier
egen dist_id=concat(id_01 id_02)
egen distcode=group(dist_id)

*define zones from 2010 DHS

gen zone=.
replace zone=1 if region==16 | region==17 | region==14 // Western zone Tabora, Shinyanga, Kigoma
replace zone=2 if region==2 | region==3 | region==4 | region==21 // northern zone: Arusha, Kili, Tanga, Manyara 
replace zone=3 if region==1 | region==13 // Central zone: Dodoma, Singida
replace zone=4 if region==11 | region==12 | region==15 // Southern highlands:  Iringa, Mbeya, Rukwa
replace zone=5 if region==18 | region==19 | region==20 // Lake zone: Kagera, Mwanza,  mara
replace zone=6 if region==5 | region==6 | region==7 // Eastern: dar, moro, pwani
replace zone=7 if region==8 | region==9 | region==10 // Southern: Lindi, Mtara, Ruvuma
*tab zone, gen(d_zone)

*gen individual zone dummies
gen western= (zone==1)
gen northern= (zone==2)
gen central= (zone==3)
gen southern_highlands= (zone==4)
gen lake= (zone==5)
gen eastern= (zone==6)
gen southern= (zone==7)

*region dummies
gen dodoma= (region==1)
gen arusha= (region==2)
gen kili= (region==3)
gen tanga= (region==4)
gen morogoro= (region==5)
gen pwani= (region==6)
gen dar= (region==7)
gen lindi= (region==8)
gen mtwara= (region==9)
gen ruvuma= (region==10)
gen iringa= (region==11)
gen mbeya= (region==12)
gen singida= (region==13)
gen tabora= (region==14)
gen rukwa= (region==15)
gen kigoma= (region==16)
gen shinyanga= (region==17)
gen kagera= (region==18)
gen mwanza= (region==19)
gen mara= (region==20)
gen manyara= (region==21)

/*
generate variable for when the ESS ITN campaign (2010-2011) occurred
*everysleeping space "ess" campaign // this data comes from the table in Renggli et al. Malaria Journal
*the campaigns took place over 3 days, here we take the middle day as the date of the campaign
*/

gen ess=td(02,10,2010) if mtwara==1 | lindi==1 | ruvuma==1 // southern zone
replace ess=td(13,11,2010) if iringa==1 | rukwa==1 | mbeya==1 // southern highlands
replace ess=td(08,1,2011) if morogoro==1 | dodoma==1 | singida==1	// central region
replace ess=td(19,02,2011) if shinyanga==1 | kigoma==1 | tabora==1 // West Lake 
replace ess=td(09,4,2011) if mwanza==1 | kagera==1 | mara==1 // Lake region
replace ess=td(11,9,2011) if pwani==1 | tanga==1 | dar==1 // //coast
replace ess=td(08,10,2011) if kili==1 | arusha==1 | manyara==1 // northern zone

format ess %td
gen time_since_ucc=ivudate-ess

/*
outcome variables: 

First stage:
anyslept_itn: did any hh member sleep under ITN?
slept_itn: did the respondent sleep under an ITN?
itn_pct: what pct of hh members slept under an ITN?

Politics: 
approval: for village chair, village exec, ward exec, MP. binary: approve_vc1, and continuous (1-5). 
*/

*create indicator for whether the observation was the individual selected to respond to governance module 
gen respondent= (gov_respondent_2010==indidy2) if !missing(gov_respondent_2010) & !missing(indidy2)
replace age=. if respondent==1 & age<16

/*
in 2010 survey, becuse of how merging was done,
 all respondents have responses for the governance module questions, 
rather than just the actual selected respondent. 
Here we create missing governace responses for the hh members who were not the actual respondents to this module
*/  
foreach v in approve_vc1 approve_vc1 approve_mp1 approve_veo1 approve_councillor1  ///
approve_weo1 approve_headmaster1 approve_extofficer1 approve_police1 approve_vc  /// 
approve_mp approve_veo approve_weo approve_extofficer approve_police approve_headmaster approve_councillor { //  frequent_reader  frequent_listener frequent_watcher 
replace `v' =. if respondent!=1
}

foreach v in approve_vc approve_mp approve_veo approve_weo approve_councillor {
replace `v'=. if `v'==6 | `v'==9
}

*But currently 1 is highest approval, 4 is highest disapproval. Reverse scale so higher is better
*also standardize this continuous variable
foreach v in approve_vc approve_mp approve_veo approve_weo approve_councillor {
gen `v'2=`v'
replace `v'2=4 if `v'==1 & !missing(`v')
replace `v'2 =3 if `v'==2 & !missing(`v')
replace `v'2 =2 if `v'==3 & !missing(`v')
replace `v'2 =1 if `v'==4 & !missing(`v')
egen st_`v'=std(`v'2)
}


*now label outcome variables
label var approve_vc1 "village chair approval"
label var approve_veo1 "village exec approval"
label var approve_weo1 "ward exec approval"
label var approve_councillor1 "councillor approval"
label var approve_mp1 "MP approval"
label var anyslept_itn "any hh member slept under ITN"
label var slept_itn "slept under ITN"
label var itn_pct "% hh members slept under ITN"
label var anynet "any hh member slept under net"
label var free_net_all "has free net"
label var free_itn_all "any hh member has free ITN"
label var free_hh_all "any hh member has free net"

*now create simplified naming conventions
gen village_chair=approve_vc1
gen village_exec=approve_veo1
gen councillor=approve_councillor1
gen ward_exec=approve_weo1
gen mp=approve_mp1
gen headmaster=approve_headmaster1
gen extension= approve_extofficer1
gen police=	approve_police1
gen cons_pc=consumption_pc

global approve_long "approve_vc1 approve_veo1 approve_weo1 approve_councillor1 approve_mp1"
global approve "village_chair village_exec councillor ward_exec mp"
global approve_cont "approve_vc approve_veo  approve_councillor approve_weo approve_mp"
global approve_cont2 "approve_vc2 approve_veo2  approve_councillor2 approve_weo2 approve_mp2"
global approve_others "headmaster extension police"
global missing "village_chair_miss village_exec_miss councillor_miss ward_exec_miss mp_miss"
global bednet "anyslept_itn slept_itn itn_pct anynet free_net free_itn free_hh voucher_net"
global bednet2 "anyslept_itn slept_itn itn_pct anynet free_net_all free_itn_all free_hh_all"
global bednet1 "itn_pct anynet free_net free_itn free_hh"
global balance "consumption_pc female age urban"
global balance1 "cons_pc female age urban"
global bandwidth "period_60 period_90 period_120 period_150 period_180"
global bandwidth2 "period_30 period_60 period_90 period_120 period_150 period_180"
global bandwidth3 "period_30 period_45 period_60 period_75 period_90"

gen sample= (time_since_ucc>-365 & time_since_ucc<365) if !missing(time_since_ucc)
gen omitperiod= (time_since_ucc>-15 & time_since_ucc<15) // since we only know the month of interview, not the day, we create a 30 day window around the ITN campaign

*******
**create indicator variables for different levels of ditrict level malaria prevalence
*******

forvalues i= 30(15)180 {
gen period_`i'= (time_since_ucc>-`i' & time_since_ucc<`i')
label var period_`i' "`i' days"
}

gen malaria10= (malaria_2000>=0 & malaria_2000<=10) & !missing(malaria_2000)
gen malaria20= (malaria_2000>10 & malaria_2000<20) & !missing(malaria_2000)
gen malaria30= (malaria_2000>20 & malaria_2000<30) & !missing(malaria_2000)
gen malaria40= (malaria_2000>30 & malaria_2000<40) & !missing(malaria_2000)
gen malaria50= (malaria_2000>40) & !missing(malaria_2000)

gen post= time_since_ucc>15 if !missing(time_since_ucc) // treatment variable
lab var post "post campaign"
gen post_incl_omit= (time_since_ucc>0) if !missing(time_since_ucc)
lab var post "post campaign (including 30 days around campaign date)"

***********
*Create a standardized version of the continuous approval variable (st_approval_vc)
*using the mean and SD of the control (post==0) values, rather than the whole sample
**********
foreach var in $approve_cont2 {
sum `var' if post==0 & period_90==1 & omitperiod==0
gen st_`var' = (`var'-r(mean))/r(sd)
}

global st_approve2 "st_approve_vc2 st_approve_veo2 st_approve_councillor2 st_approve_weo2 st_approve_mp2"

***********
*generate a placebo "treatment" based on  the dates of ITN campaign training visits
***********

gen registration_visit=td(07,07,2010) if mtwara==1 | lindi==1 | ruvuma // southern zone, July 5-9th 2010
replace registration_visit=td(15,09,2010) if iringa==1 | rukwa==1 | mbeya==1 // southern highlands, Sept 13-17th
replace registration_visit=td(27,10,2010) if morogoro==1 | dodoma | singida	// central region, Oct 25-29 2010
replace registration_visit=td(24,11,2011) if shinyanga==1 | kigoma==1 | tabora // West Lake,  NOv 22-26 2010
replace registration_visit=td(05,01,2011) if mwanza==1 | kagera==1 | mara==1 // Lake region, Jan 3-7th 2011
replace registration_visit=td(26,01,2011) if pwani==1 | tanga==1 | dar==1 // Coast, Jan 24-28 2011
replace registration_visit=td(23,02,2011) if kili==1 | arusha==1 | manyara==1 // northern zone, Feb 21-25 2011

format registration_visit %td
gen time_since_reg=ivudate-registration_visit

gen reg_post= time_since_reg>15 if !missing(time_since_reg) // 
gen reg_omitperiod= (time_since_reg>-15 & time_since_reg<15) // 
gen reg_period= (time_since_reg>-90 & time_since_reg<90) // 90 day bandwidth on either side

**************
 *generate interaction terms
************
 
forvalues i=0(25)100 {
foreach v in ccm chadema cuf other {
 gen post_`v'_pct_`i'=post*`v'_pct_`i'
 }
 }

gen post_opposition=post*opposition_50_2010
gen post_ccm_100=post*ccm_100_2010
label var post_ccm_100 "post campaign x CCM 100% in 2010"
gen post_ccm_pct=post*ccm_pct
label var post_ccm_pct "post campaign x CCM %"
gen post_ccmvote08=post*ccm_vote_lag
lab var post_ccmvote08 "treat x lagged CCM support"

*generate missing indicators
foreach y in $approve {
gen `y'_miss= (`y'==.) & respondent==1
}

*generate malaria indicators
gen post_high_malaria=post*above_median if !missing(above_median)
lab var post_high_malaria "treatment x above median malaria district"
gen high_malaria=above_median

*********
*now generate placebo reform +/2 100 and 200 days before and after actaul campaign
*********

gen placebo100=ess+100 
gen time_since_placebo100=ivudate-placebo100
gen post_placebo100= (time_since_placebo100>15) if !missing(time_since_placebo100)
gen placebo100_omit= (time_since_placebo100>-15 & time_since_placebo100<15)
gen placebo100_sample=(time_since_placebo100>-900 & time_since_placebo100<90) 

gen placebo200=ess+200
gen time_since_placebo200=ivudate-placebo200
gen post_placebo200= (time_since_placebo200>15) if !missing(time_since_placebo200)
gen placebo200_omit= (time_since_placebo200>-15 & time_since_placebo200<15)
gen placebo200_sample=(time_since_placebo200>-90 & time_since_placebo200<90) 

gen placebo_neg100=ess-100
gen time_since_placebo_neg100=ivudate-placebo_neg100
gen post_placebo_neg100= time_since_placebo_neg100>15 if !missing(time_since_placebo_neg100)
gen placebo_neg100_omit= (time_since_placebo_neg100>-15 & time_since_placebo_neg100<15)
gen placebo_neg100_sample=(time_since_placebo_neg100>-90 & time_since_placebo_neg100<90) 

gen placebo_neg200=ess-200 
gen time_since_placebo_neg200=ivudate-placebo_neg200
gen post_placebo_neg200= time_since_placebo_neg200>15 if !missing(time_since_placebo_neg200)
gen placebo_neg200_omit= (time_since_placebo_neg200>-15 & time_since_placebo_neg200<15)
gen placebo_neg200_sample=(time_since_placebo_neg200>-90 & time_since_placebo_neg200<90) 

**************
*now generate time trend variables, drop unneeded variables, and save final file
**************

gen post_trend=post*time_since_ucc
gen time2=time_since_ucc^2

drop ccm1 - party_other37
drop ccm_vill_lead1 - other_vill_lead37

save "constructed data/for_analysis.dta", replace
